package com.bawei.caohao20240805.view.fragment

import android.content.Context
import android.content.Intent
import android.net.ConnectivityManager
import android.os.Bundle
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.TextView
import android.widget.Toast
import androidx.lifecycle.Observer
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.LinearLayoutManager
import coil.load
import coil.transform.RoundedCornersTransformation
import com.ayvytr.adapter.smart
import com.bawei.caohao20240805.MyApp
import com.bawei.caohao20240805.R
import com.bawei.caohao20240805.base.BaseFragment
import com.bawei.caohao20240805.databinding.FragmentLoginBinding
import com.bawei.caohao20240805.view.activity.ZBActivity
import com.bawei.caohao20240805.viewmodel.LoginVIewModel
import com.bawei.comm.model.bean.LeiBean
import com.google.android.material.tabs.TabLayout
import com.google.android.material.tabs.TabLayout.OnTabSelectedListener
import com.stx.xhb.xbanner.XBanner

//7．图1情感列表模块使用Databinding技术
//8．图1情感列表模块使用ViewModel技术
class LoginFragment : BaseFragment<LoginVIewModel, FragmentLoginBinding>(R.layout.fragment_login) {
    val leiadapter =
        smart(mutableListOf<LeiBean.Result>(), R.layout.lei_mo, { item, position ->
            findViewById<ImageView>(R.id.img).load(item.liveCoverUrl) {
                error(R.mipmap.ic_launcher)
                placeholder(R.mipmap.ic_launcher)
                    transformations(RoundedCornersTransformation(100f))
            }
            findViewById<TextView>(R.id.name).text = item.nickName
        }) { }
    val leiadapter2 =
        smart(mutableListOf<LeiBean.Result>(), R.layout.lei_mo2, { item, position ->
            findViewById<ImageView>(R.id.img).load(item.liveCoverUrl) {
                error(R.mipmap.ic_launcher)
                placeholder(R.mipmap.ic_launcher)
            }
            findViewById<TextView>(R.id.name).text = item.nickName
            if (item.liveStatus == 1) {
                findViewById<TextView>(R.id.type).text = "直播中"
            } else {
                findViewById<TextView>(R.id.type).text = "录播"
            }
        }) { }.click { item, position ->
            startActivity(Intent(context,ZBActivity::class.java).putExtra("id",item.liveId).putExtra("type",item.liveStatus))
        }

    override fun dataObSer() {
        mViewModel.leidata.observe(this, Observer {
//            3．完成图1首页标题布局
//4．完成图1首页轮播图
            if (it.result != null) {
                mDing.xb.removeAllViews()
                mDing.xb.setData(it.result, null)
                mDing.xb.setmAutoPalyTime(3000)
                mDing.xb.setmAdapter(XBanner.XBannerAdapter { banner, view, position ->
                    (view as ImageView).load(it.result[position].liveCoverUrl)
                })

                leiadapter.update(it.result,true)
                leiadapter2.update(it.result, true)
            }
        })

    }


    override fun initView() {
//        点击图1右上角视频图标进入直播页面
        mDing.she.setOnClickListener {
            startActivity(Intent(context, ZBActivity::class.java))
        }
        var manager =
            context?.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager
        var networkInfo = manager.getNetworkInfo(ConnectivityManager.TYPE_WIFI)
//        16．断网提示用户
        if (networkInfo != null && networkInfo.isConnected) {
            Toast.makeText(context, "网络已连接", Toast.LENGTH_SHORT).show()
        } else {
            Toast.makeText(context, "请连接网络", Toast.LENGTH_SHORT).show()
            return
        }
        val title = arrayOf("推荐", "女神", "男神", "听歌", "情感", "游戏")
        for (s in title) {
            mDing.tab.addTab(mDing.tab.newTab().setText(s))
        }
        mDing.tab.addOnTabSelectedListener(object : OnTabSelectedListener {
            override fun onTabSelected(tab: TabLayout.Tab?) {
                if (tab?.position == 0) {
                    leiadapter2.clear()
                    mViewModel.getlei(MyApp.userId, 100, 1, 21)
                    mDing.rec.visibility=View.VISIBLE
                    mDing.rec2.visibility=View.GONE
                }
                if(tab?.position==1){
                    leiadapter2.clear()
                    mViewModel.getlei(MyApp.userId, 102, 1, 21)
                    mDing.rec.visibility=View.VISIBLE
                    mDing.rec2.visibility=View.GONE
                }
                if(tab?.position==2){
                    leiadapter2.clear()
                    mViewModel.getlei(MyApp.userId, 101, 1, 21)
                    mDing.rec.visibility=View.VISIBLE
                    mDing.rec2.visibility=View.GONE
                }
                if (tab?.position == 4) {
                    leiadapter2.clear()
                    mViewModel.getlei(MyApp.userId, 104, 1, 21)
                    mDing.rec.visibility=View.GONE
                    mDing.rec2.visibility=View.VISIBLE
                }
            }

            override fun onTabUnselected(tab: TabLayout.Tab?) {

            }

            override fun onTabReselected(tab: TabLayout.Tab?) {

            }

        })

//6．完成图1首页推荐模块网格
        mViewModel.getlei(MyApp.userId, 101, 1, 11)
        mDing.rec.adapter = leiadapter2
        mDing.rec.layoutManager = GridLayoutManager(context, 2)
        mDing.rec2.adapter = leiadapter
//6．完成图1首页推荐模块网格
        mDing.rec2.layoutManager = LinearLayoutManager(context)

    }

}